//login.js
//获取应用实例
var app = getApp();
var md5 = require("../../utils/md5.js");

Page({
  data: {
    remind: '加载中',
    angle: 0,
    Title: '中诺科技',
    shop_id: 0
  },
  goToIndex: function() {
    wx.switchTab({
      url: '/pages/jingxuan/index',
    });
  },
  onLoad: function(options) {
    var that = this
    // console.log(options)
    // console.log(options.q)
    // console.log(decodeURIComponent(options.q))
    // console.log(options.shop_id)
    //console.log(options.openid)
    //console.log(options.user_id)
    var shop_url = decodeURIComponent(options.scene);
    var info = shop_url.indexOf('=');
    var ORshop_id = shop_url.slice(info+1)
    // console.log(ORshop_id)
    // console.log('打印二维码取值')



    var openid = options.openid;
    var user_id = options.user_id;
    if (openid != undefined && user_id != undefined) {
      app.globalData.openId = openid
      app.globalData.userId = user_id
      wx.setStorageSync('openId', openid);
      wx.setStorageSync('userId', user_id);
    }


    var service = 'User.GetShopId';
    var sign = md5.md5(service);
    wx.request({
      url: app.globalData.serviceUrl + 'User.GetShopId',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
      },
      success: function(res) {
        if (options.scene == undefined){
          if (options.shop_id == undefined) {
            if (res.data.data.info.shop_id > 0) {
              app.globalData.shopId = res.data.data.info.shop_id
            } else {
              var sync_shop_id = wx.getStorageSync('shop_id')
              if (sync_shop_id == '') {
                app.globalData.shopId = 1066
              } else {
                app.globalData.shopId = sync_shop_id
              }
            }
          } else {
            var shop_id = options.shop_id;
            app.globalData.shopId = shop_id;
            wx.setStorageSync('shop_id', shop_id)
          }
        }else{
          app.globalData.shopId = ORshop_id;
          wx.setStorageSync('shop_id', ORshop_id)
        }

        console.log(app.globalData.shopId);
        that.editLand();
        var service = 'Shop.GetShopInfo';
        var openid = app.globalData.openId;
        var user_id = app.globalData.userId;
        var shop_id = app.globalData.shopId;
        console.log(654321);
        console.log(user_id);
        console.log(openid);
        console.log(shop_id);
        var sign = md5.md5(openid + service + shop_id + user_id);
        wx.request({
          url: app.globalData.serviceUrl + service,
          header: {
            'Content-Type': 'json'
          },
          data: {
            sign: sign,
            user_id: user_id,
            openid: openid,
            shop_id: shop_id,
          },
          success: (res) => {
            console.log(123456);
            console.log(res);
            var result = res.data.data;
            if (result.code == 0) {
              var shop_address_jingwei = result.info.shop_address_jingwei;
              var jingwei = shop_address_jingwei.split(",");
              app.globalData.shopPic = result.info.shop_pic_small;
              app.globalData.shopName = result.info.shop_name;
              that.setData({
                shop_name: result.info.shop_name,
                shop_address: result.info.shop_address,
                shop_pic_small: result.info.shop_pic_small,
              })
            } else {
              wx.showToast({
                title: '获取失败',
                icon: 'success',
                duration: 2000
              })
            }
          },
        })

      }
    })



  },
  onShow: function() {

  },
  onReady: function() {
    var that = this;

    setTimeout(function() {
      that.setData({
        remind: ''
      });
    }, 1000);
    wx.onAccelerometerChange(function(res) {
      var angle = -(res.x * 30).toFixed(1);
      if (angle > 14) {
        angle = 14;
      } else if (angle < -14) {
        angle = -14;
      }
      if (that.data.angle !== angle) {
        that.setData({
          angle: angle
        });
      }
    });
  },

  editLand: function() {
    var service = 'User.EditLand';
    var open_id = app.globalData.openId;
    var user_id = app.globalData.userId;
    var pid = 1;
    var sign = md5.md5(open_id + pid + service + user_id);
    wx.request({
      url: app.globalData.serviceUrl + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        open_id: open_id,
        pid: pid,
      },
      success: (res) => {
        var result = res.data.data;
        if (result.code == 0) {
          if (result.info == 1) {
            console.log('登陆成功，1111111')
          }
        }
      }
    })
  }
});